package com.yb.web.global.servlet;

import java.io.IOException;
import java.util.Iterator;
import java.util.Vector;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.yb.util.framework.OnLineCustomers;
import com.yb.util.framework.UserList;

public class InitUsersIp implements Filter {

	public void destroy() {
	}

	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpSession session = request.getSession();
		UserList manage = UserList.getInstance();
		Vector<OnLineCustomers> vo = manage.getList();
		for (Iterator<OnLineCustomers> iterator = vo.iterator(); iterator.hasNext();) {
			OnLineCustomers oc = (OnLineCustomers) iterator.next();
			if(oc.getSessionId().equals(session.getId())){
				oc.setIp(request.getRemoteAddr());
				break;
			}
		}
		chain.doFilter(request, resp);
	}

	public void init(FilterConfig cfg) throws ServletException {
	}

}
